// index.js
Page({
  data: {
    loginDisabled: false,
    userName: '',
    userAvatar: '',
    joinDays: null,
    score: null,
    rewardsNum: null,
    joinTime: null
  },

  onLoad: function (event) {
    if (getApp().globalData.wxusername == '') {
      this.setData({
        loginDisabled: true
      })
    }
    this.getJoinDetail()
  },

  onShow: function (event) {
    if (getApp().globalData.wxusername == '') {
      this.setData({
        loginDisabled: true
      })
    }
    this.setData({
      userName: getApp().globalData.wxusername,
      userAvatar: getApp().globalData.wxuseravatar
    })
    this.getJoinDetail()
  },

  onHide: function (event) {
    if (wx.getStorageSync('nickName') != null) {
      this.setData({
        loginDisabled: false
      })
    }
    this.getJoinDetail()
  },

  // 联系我们
  goContactUs: function (event) {
    if (this.data.loginDisabled) {
      return
    }
    wx.showModal({
      title: '联系邮箱',
      content: 'sir_21@163.com',
      showCancel: false
    });
  },

  // 登录
  userLogin: function (event) {
    // 获取用户基本信息
    const that = this
    wx.getUserProfile({
      desc: '用于登录',
      success: res => {
        // 在成功回调函数中处理获取用户信息的逻辑
        var nickName = res.userInfo.nickName;
        var avatarUrl = res.userInfo.avatarUrl;
        // 进行后续操作
        // 获取js_code
        wx.login({
          success: (res) => {
            wx.request({
              url: getApp().globalData.commonIP + '/wxuser/login',
              method: 'POST',
              data: {
                wxcode: res.code,
                nickName: nickName,
                avatarUrl: avatarUrl
              },
              header: {
                'content-type': 'application/json' // 默认值
              },
              success(res) {
                if (res.data.msg == '登录成功') {
                  wx.setStorageSync('token', res.data.data.token)
                  wx.setStorageSync('nickName', nickName)
                  wx.setStorageSync('avatarUrl', avatarUrl)
                  that.setData({
                    loginDisabled: false,
                    userName: nickName,
                    userAvatar: avatarUrl
                  })
                  getApp().globalData.wxusername = nickName
                  getApp().globalData.wxuseravatar = avatarUrl
                } else {
                  wx.showModal({
                    title: '登录失败',
                    content: res.data.msg,
                    showCancel: false
                  });
                }
              }
            })
          }
        })
      }
    })
  },

  // 注销
  userLogout: function (event) {
    var that = this
    wx.showModal({
      title: '提示',
      content: '确定要退出登录吗？',
      complete: (res) => {
        if (res.cancel) {

        }

        if (res.confirm) {
          wx.request({
            url: getApp().globalData.commonIP + '/wxuser/logout',
            method: 'GET',
            header: {
              'content-type': 'application/json',
              'token': wx.getStorageSync('token')
            },
            data: {
              nickName: wx.getStorageSync('nickName')
            },
            success: res => {
              if (res.data.msg == "token过期，请重新登录" || res.data.msg == "token失效，请重新登录" || res.data.msg == "无效签名，请重新登录" || res.data.msg == "token算法不一致，请重新登录") {
                wx.showModal({
                  title: '提示',
                  content: res.data.msg,
                  showCancel: false,
                  complete: (res) => {
                    if (res.cancel) {

                    }

                    if (res.confirm) {
                      // 重置全局变量和去除缓存
                      getApp().globalData.wxusername = ''
                      getApp().globalData.wxuseravatar = ''
                      wx.removeStorageSync('nickName')
                      wx.removeStorageSync('avatarUrl')
                      wx.removeStorageSync('token')

                      wx.switchTab({
                        url: '/pages/index/index'
                      })
                    }
                  }
                })
                return
              }
              if (res.data.msg == '操作成功') {
                if (getApp().globalData.wxusername != '') {
                  // 重置全局变量和去除缓存
                  getApp().globalData.wxusername = ''
                  getApp().globalData.wxuseravatar = ''
                  wx.removeStorageSync('nickName')
                  wx.removeStorageSync('avatarUrl')
                  wx.removeStorageSync('token')
                  // 重置局部变量
                  that.setData({
                    loginDisabled: true,
                    userName: '游客',
                    userAvatar: null
                  })
                }
              } else {
                wx.showModal({
                  title: '提示',
                  content: res.data.msg,
                  showCancel: false
                })
              }
            }
          })
        }
      }
    })
  },

  goJoinInfo() {
    if (this.data.loginDisabled) {
      return
    }
    wx.showModal({
      title: '提示',
      content: '您于' + this.data.joinTime + '加入本平台，距今已加入了' + this.data.joinDays + '天。\n感谢您一直以来的陪伴和支持~',
      showCancel: false
    })
  },

  getJoinDetail() {
    var that = this
    wx.request({
      url: getApp().globalData.commonIP + '/wxuser/getJoinTimeByNickName',
      method: 'GET',
      header: {
        'content-type': 'application/json',
        'token': wx.getStorageSync('token')
      },
      data: {
        nickName: wx.getStorageSync('nickName')
      },
      success: res => {
        if (res.data.msg == "token过期，请重新登录" || res.data.msg == "token失效，请重新登录" || res.data.msg == "无效签名，请重新登录" || res.data.msg == "token算法不一致，请重新登录") {
          wx.showModal({
            title: '提示',
            content: res.data.msg,
            showCancel: false,
            complete: (res) => {
              if (res.cancel) {

              }

              if (res.confirm) {
                // 重置全局变量和去除缓存
                getApp().globalData.wxusername = ''
                getApp().globalData.wxuseravatar = ''
                wx.removeStorageSync('nickName')
                wx.removeStorageSync('avatarUrl')
                wx.removeStorageSync('token')

                wx.switchTab({
                  url: '/pages/index/index'
                })
              }
            }
          })
          return
        }
        if (res.data.msg == '操作成功') {
          that.setData({
            joinTime: res.data.data.joinTime,
            joinDays: res.data.data.joinDays,
            score: res.data.data.score,
            rewardsNum: res.data.data.rewardsNum
          })
        } else {
          wx.showModal({
            title: '提示',
            content: res.data.msg,
            showCancel: false
          })
        }
      }
    })
  },

  goScoreDetail() {
    if (this.data.loginDisabled) {
      return
    }
    wx.showModal({
      title: '提示',
      content: '您当前的积分数量为' + this.data.score + '，可用于兑换奖券哦~',
      showCancel: false
    })
  },

  goRewardsDetail() {
    if (this.data.loginDisabled) {
      return
    }
    wx.navigateTo({
      url: '/pages/rewards/index'
    })
  },

  goDispatchHistory() {
    if (this.data.loginDisabled) {
      return
    }
    wx.navigateTo({
      url: '/pages/my/history/index?historyName=调度历史'
    })
  },

  goRequestHistory() {
    if (this.data.loginDisabled) {
      return
    }
    wx.navigateTo({
      url: '/pages/my/history/index?historyName=请求历史'
    })
  },

  goRewardsHistory() {
    if (this.data.loginDisabled) {
      return
    }
    wx.navigateTo({
      url: '/pages/my/history/index?historyName=奖券兑换'
    })
  }
})